﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class KPDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitializeFormData();
            }
        }
        private void InitializeFormData()
        {
            ApplicationService applicationService = new ApplicationService();
            ApplicationForm applicationForm = applicationService.GetApplicationFormByID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            rmaCodeLabel.Text = applicationForm.ApplicationCode;
            runningNOLbl.Text = applicationForm.RunningNumber;
            if (!applicationForm.UserReference.IsLoaded)
            {
                applicationForm.UserReference.Load();
            }
            if (!applicationForm.GatheringReference.IsLoaded)
            {
                applicationForm.GatheringReference.Load();
            }
            companyNameLabel.Text = applicationForm.User.CompanyName;
            quatationDateTimeLbl.Text = applicationForm.QuatationDatetime.HasValue ? applicationForm.QuatationDatetime.Value.ToString("yyyy-MM-dd") : "";
            companyAddressLbl.Text = applicationForm.User.UserAddress;
            receiveMoneyDateTimeLbl.Text = applicationForm.ReceiveDateTime.HasValue ? applicationForm.Gathering.PayDateTime.Value.ToString("yyyy-MM-dd") : "";
            connectPersonLabel.Text = applicationForm.User.ContactPerson;
            faxLbl.Text = applicationForm.User.UserFax;
            phoneNumberLabel.Text = applicationForm.User.UserPhone;
            emailLbl.Text = applicationForm.User.UserEmail;

            if (!applicationForm.GatheringReference.IsLoaded)
            {
                applicationForm.GatheringReference.Load();
            }

            ApplicationProductService productService = new ApplicationProductService();
            List<ApplicationProduct> products = productService.GetApplicatonProductByFormID(applicationForm.ApplicationID);
            //List<ApplicationProduct> products = productService.GetApplicatonProductSendFaPiaoByFormID(applicationForm.ApplicationID).ToList();
            productGridView.DataSource = products.Where(p => p.IsDecideToRepair.HasValue && p.IsDecideToRepair.Value == true && p.IsQuotePrice.HasValue && p.IsQuotePrice.Value == true);
            productGridView.DataBind();
        }

        protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }
    }
}
